package com.ruoyi.blog.controller;

import com.ruoyi.blog.core.controller.BaseController;
import com.ruoyi.blog.core.domain.AjaxResult;
import com.ruoyi.blog.domain.BlogAvatarBorder;
import com.ruoyi.blog.domain.vo.BlogAvatarBorderVo;
import com.ruoyi.blog.service.BlogAvatarBorderService;
import com.ruoyi.blog.utils.StringUtils;
import com.ruoyi.file.constant.FileProjectIdConstant;
import com.ruoyi.file.domain.po.FileProCol;
import com.ruoyi.file.service.FileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 依梦
 * @Date: 2024/12/7
 * @Description: BlogAvatarBorderController
 */
@RestController
@RequestMapping("/blog/avatar_border")
public class BlogAvatarBorderController extends BaseController {

    @Resource
    private BlogAvatarBorderService blogAvatarBorderService;
    @Resource
    private FileService fileService;

    @GetMapping("/list")
    public AjaxResult list() {
        // 获取所有的头像信息
        List<BlogAvatarBorderVo> list = blogAvatarBorderService.getList(null);
        list.forEach((item) -> {
            // 进行 copy 到 BlogArticlesVo 中
            FileProCol fileProCol = fileService.getOneFileInfo(FileProjectIdConstant.PROJECT_AVATAR_BORDER, 0L, Long.valueOf(item.getId()));
            item.setFile(fileProCol);
        });
        return AjaxResult.success(list);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody BlogAvatarBorder blogAvatarBorder) {
        return toAjax(blogAvatarBorderService.save(blogAvatarBorder));
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody BlogAvatarBorder blogAvatarBorder) {
        return toAjax(blogAvatarBorderService.updateById(blogAvatarBorder));
    }

    @DeleteMapping("/delete")
    public AjaxResult delete(Long id) {
        return toAjax(blogAvatarBorderService.removeById(id));
    }

    @PostMapping("/sync")
    public AjaxResult sync() {
        BlogAvatarBorder blogAvatarBorder = new BlogAvatarBorder();
        blogAvatarBorder.setStatus(0);
        // 获取所有的头像信息
        List<BlogAvatarBorderVo> list = blogAvatarBorderService.getList(blogAvatarBorder);
        list.forEach((item) -> {
            // 进行 copy 到 BlogArticlesVo 中
            FileProCol fileProCol = fileService.getOneFileInfo(FileProjectIdConstant.PROJECT_AVATAR_BORDER, 0L, Long.valueOf(item.getId()));
            item.setFile(fileProCol);
        });
        // 同步 url
        return AjaxResult.success(blogAvatarBorderService.sync(list));
    }

    @GetMapping("/all")
    public AjaxResult getAll() {
        BlogAvatarBorder blogAvatarBorder = new BlogAvatarBorder();
        blogAvatarBorder.setStatus(0);
        return AjaxResult.success(blogAvatarBorderService.getList(blogAvatarBorder));
    }

}
